package Form;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.Calendar;
import java.util.Date;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSpinner;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SpinnerDateModel;
import javax.swing.SpinnerNumberModel;

import Control.CommonControls;
import Control.Workshop_CreateWorkshop_Controller;

@SuppressWarnings("serial")
public class Workshop_CreateWorkshop_Form extends JPanel{
	//Class Attributes
	private JLabel lblWorkshopId;
	private JTextField workshopName;
	private JTextField trainerName;
	private JTextField contactNo;
	private JTextField venue;
	private JTextArea description;
	private JSpinner startDateTime;
	private JSpinner endDateTime;
	private JSpinner capacity;
	private JComboBox category;
	private JComboBox trainerNameCombo;
	private JCheckBox newTrainer;
	private JButton btnCreate;
	private JLabel lblBG;
	private JLabel lblGrayBG;

	//Object Attributes
	private Workshop_WorkshopMain_Form parser;
	private Workshop_CreateWorkshop_Controller control;
	
	public Workshop_WorkshopMain_Form getParser() {
		return parser;
	}

	public void setParser(Workshop_WorkshopMain_Form parser) {
		this.parser = parser;
	}

	public Workshop_CreateWorkshop_Controller getControl() {
		return control;
	}

	public void setControl(Workshop_CreateWorkshop_Controller control) {
		this.control = control;
	}

	public Workshop_CreateWorkshop_Form(Workshop_WorkshopMain_Form parser){
		this.setParser(parser);
		
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[] {0, 0, 67, 68, 38, 68, 67, 0};
		gridBagLayout.rowHeights = new int[] {35, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
		gridBagLayout.columnWeights = new double[]{1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0};
		gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0};
		setLayout(gridBagLayout);
		
		
		JLabel lblTitle = new JLabel("Create Workshop");
		lblTitle.setForeground(Color.LIGHT_GRAY);
		lblTitle.setFont(new Font("Rockwell", Font.BOLD, 28));
		GridBagConstraints gbc_lblTitle = new GridBagConstraints();
		gbc_lblTitle.anchor = GridBagConstraints.WEST;
		gbc_lblTitle.gridwidth = 2;
		gbc_lblTitle.fill = GridBagConstraints.HORIZONTAL;
		gbc_lblTitle.insets = new Insets(0, 0, 2, 5);
		gbc_lblTitle.gridx = 1;
		gbc_lblTitle.gridy = 0;
		add(lblTitle, gbc_lblTitle);
		
		getLblWorkshopId();
		GridBagConstraints gbc_lblWorkshopId = new GridBagConstraints();
		gbc_lblWorkshopId.gridwidth = 2;
		gbc_lblWorkshopId.anchor = GridBagConstraints.EAST;
		gbc_lblWorkshopId.insets = new Insets(0, 0, 5, 5);
		gbc_lblWorkshopId.gridx = 5;
		gbc_lblWorkshopId.gridy = 0;
		add(getLblWorkshopId(), gbc_lblWorkshopId);
		
		JLabel lblCategory = new JLabel("Category:");
		lblCategory.setFont(new Font("Tahoma", Font.BOLD, 14));
		lblCategory.setForeground(Color.WHITE);
		lblCategory.setBackground(new Color(0, 0, 0, 180));
		lblCategory.setOpaque(true);
		GridBagConstraints gbc_lblCategory = new GridBagConstraints();
		gbc_lblCategory.anchor = GridBagConstraints.EAST;
		gbc_lblCategory.insets = new Insets(0, 0, 5, 5);
		gbc_lblCategory.gridx = 1;
		gbc_lblCategory.gridy = 1;
		add(lblCategory, gbc_lblCategory);
		
		getCategory();
		GridBagConstraints gbc_category = new GridBagConstraints();
		gbc_category.gridwidth = 2;
		gbc_category.insets = new Insets(0, 0, 5, 5);
		gbc_category.fill = GridBagConstraints.HORIZONTAL;
		gbc_category.gridx = 2;
		gbc_category.gridy = 1;
		add(getCategory(), gbc_category);
		
		JLabel lblWorkshopName = new JLabel("Workshop Name:");
		lblWorkshopName.setFont(new Font("Tahoma", Font.BOLD, 14));
		lblWorkshopName.setForeground(Color.WHITE);
		lblWorkshopName.setBackground(new Color(0, 0, 0, 180));
		lblWorkshopName.setOpaque(true);
		GridBagConstraints gbc_lblWorkshopName = new GridBagConstraints();
		gbc_lblWorkshopName.anchor = GridBagConstraints.EAST;
		gbc_lblWorkshopName.insets = new Insets(0, 0, 5, 5);
		gbc_lblWorkshopName.gridx = 1;
		gbc_lblWorkshopName.gridy = 2;
		add(lblWorkshopName, gbc_lblWorkshopName);
		
		getWorkshopName();
		GridBagConstraints gbc_workshopName = new GridBagConstraints();
		gbc_workshopName.gridwidth = 2;
		gbc_workshopName.fill = GridBagConstraints.HORIZONTAL;
		gbc_workshopName.insets = new Insets(0, 0, 5, 5);
		gbc_workshopName.gridx = 2;
		gbc_workshopName.gridy = 2;
		add(getWorkshopName(), gbc_workshopName);
		workshopName.setColumns(10);
		
		JScrollPane scrollPane = new JScrollPane();
		GridBagConstraints gbc_scrollPane = new GridBagConstraints();
		gbc_scrollPane.ipady = 50;
		gbc_scrollPane.gridwidth = 5;
		gbc_scrollPane.insets = new Insets(0, 0, 5, 5);
		gbc_scrollPane.fill = GridBagConstraints.BOTH;
		gbc_scrollPane.gridx = 2;
		gbc_scrollPane.gridy = 3;
		add(scrollPane, gbc_scrollPane);
		
		getDescription();
		scrollPane.setViewportView(getDescription());
		
		JLabel lblVenue = new JLabel("Venue:");
		lblVenue.setFont(new Font("Tahoma", Font.BOLD, 14));
		lblVenue.setForeground(Color.WHITE);
		lblVenue.setBackground(new Color(0, 0, 0, 180));
		lblVenue.setOpaque(true);
		GridBagConstraints gbc_lblVenue = new GridBagConstraints();
		gbc_lblVenue.anchor = GridBagConstraints.EAST;
		gbc_lblVenue.insets = new Insets(0, 0, 5, 5);
		gbc_lblVenue.gridx = 1;
		gbc_lblVenue.gridy = 4;
		add(lblVenue, gbc_lblVenue);
		
		getVenue();
		GridBagConstraints gbc_venue = new GridBagConstraints();
		gbc_venue.gridwidth = 2;
		gbc_venue.insets = new Insets(0, 0, 5, 5);
		gbc_venue.fill = GridBagConstraints.HORIZONTAL;
		gbc_venue.gridx = 2;
		gbc_venue.gridy = 4;
		add(getVenue(), gbc_venue); 
		
		JLabel lblTrainer = new JLabel("Trainer:");
		lblTrainer.setFont(new Font("Tahoma", Font.BOLD, 14));
		lblTrainer.setForeground(Color.WHITE);
		lblTrainer.setBackground(new Color(0, 0, 0, 180));
		lblTrainer.setOpaque(true);
		GridBagConstraints gbc_lblTrainer = new GridBagConstraints();
		gbc_lblTrainer.anchor = GridBagConstraints.EAST;
		gbc_lblTrainer.insets = new Insets(0, 0, 5, 5);
		gbc_lblTrainer.gridx = 1;
		gbc_lblTrainer.gridy = 7;
		add(lblTrainer, gbc_lblTrainer);
		
		JLabel lblCapacity = new JLabel("Capacity:");
		lblCapacity.setFont(new Font("Tahoma", Font.BOLD, 14));
		lblCapacity.setForeground(Color.WHITE);
		lblCapacity.setBackground(new Color(0, 0, 0, 180));
		lblCapacity.setOpaque(true);
		GridBagConstraints gbc_lblCapacity = new GridBagConstraints();
		gbc_lblCapacity.anchor = GridBagConstraints.EAST;
		gbc_lblCapacity.insets = new Insets(0, 0, 5, 5);
		gbc_lblCapacity.gridx = 1;
		gbc_lblCapacity.gridy = 6;
		add(lblCapacity, gbc_lblCapacity);
		
		JLabel lblDatetime = new JLabel("Date/Time");
		lblDatetime.setFont(new Font("Tahoma", Font.BOLD, 14));
		lblDatetime.setForeground(Color.WHITE);
		lblDatetime.setBackground(new Color(0, 0, 0, 180));
		lblDatetime.setOpaque(true);
		GridBagConstraints gbc_lblDatetime = new GridBagConstraints();
		gbc_lblDatetime.anchor = GridBagConstraints.EAST;
		gbc_lblDatetime.insets = new Insets(0, 0, 5, 5);
		gbc_lblDatetime.gridx = 1;
		gbc_lblDatetime.gridy = 5;
		add(lblDatetime, gbc_lblDatetime);
		
		JLabel lblDescription = new JLabel("Description:");
		lblDescription.setFont(new Font("Tahoma", Font.BOLD, 14));
		lblDescription.setForeground(Color.WHITE);
		lblDescription.setBackground(new Color(0, 0, 0, 180));
		lblDescription.setOpaque(true);
		GridBagConstraints gbc_lblDescription = new GridBagConstraints();
		gbc_lblDescription.anchor = GridBagConstraints.NORTHEAST;
		gbc_lblDescription.insets = new Insets(0, 0, 5, 5);
		gbc_lblDescription.gridx = 1;
		gbc_lblDescription.gridy = 3;
		add(lblDescription, gbc_lblDescription);
		
		getStartDateTime();
		GridBagConstraints gbc_startDateTime = new GridBagConstraints();
		gbc_startDateTime.gridwidth = 2;
		gbc_startDateTime.fill = GridBagConstraints.HORIZONTAL;
		gbc_startDateTime.insets = new Insets(0, 0, 5, 5);
		gbc_startDateTime.gridx = 2;
		gbc_startDateTime.gridy = 5;
		add(getStartDateTime(), gbc_startDateTime);
		
		JLabel lblTo = new JLabel("to");
		lblTo.setFont(new Font("Tahoma", Font.BOLD, 14));
		lblTo.setForeground(Color.WHITE);
		lblTo.setBackground(new Color(0, 0, 0, 180));
		lblTo.setOpaque(true);
		GridBagConstraints gbc_lblTo = new GridBagConstraints();
		gbc_lblTo.insets = new Insets(0, 0, 5, 5);
		gbc_lblTo.gridx = 4;
		gbc_lblTo.gridy = 5;
		add(lblTo, gbc_lblTo);
		
		getEndDateTime();
		GridBagConstraints gbc_endDateTime = new GridBagConstraints();
		gbc_endDateTime.gridwidth = 2;
		gbc_endDateTime.fill = GridBagConstraints.HORIZONTAL;
		gbc_endDateTime.insets = new Insets(0, 0, 5, 5);
		gbc_endDateTime.gridx = 5;
		gbc_endDateTime.gridy = 5;
		add(getEndDateTime(), gbc_endDateTime);
		
		getCapacity();
		GridBagConstraints gbc_capacity = new GridBagConstraints();
		gbc_capacity.fill = GridBagConstraints.HORIZONTAL;
		gbc_capacity.anchor = GridBagConstraints.WEST;
		gbc_capacity.insets = new Insets(0, 0, 5, 5);
		gbc_capacity.gridx = 2;
		gbc_capacity.gridy = 6;
		add(getCapacity(), gbc_capacity);
		
		getTrainerNameCombo();
		GridBagConstraints gbc_trainerNameCombo = new GridBagConstraints();
		gbc_trainerNameCombo.gridwidth = 2;
		gbc_trainerNameCombo.insets = new Insets(0, 0, 5, 5);
		gbc_trainerNameCombo.fill = GridBagConstraints.HORIZONTAL;
		gbc_trainerNameCombo.gridx = 2;
		gbc_trainerNameCombo.gridy = 7;
		add(getTrainerNameCombo(), gbc_trainerNameCombo);
		
		getNewTrainer();
		GridBagConstraints gbc_newTrainer = new GridBagConstraints();
		gbc_newTrainer.anchor = GridBagConstraints.WEST;
		gbc_newTrainer.gridwidth = 3;
		gbc_newTrainer.insets = new Insets(0, 0, 5, 5);
		gbc_newTrainer.gridx = 4;
		gbc_newTrainer.gridy = 7;
		add(getNewTrainer(), gbc_newTrainer);
		
		JLabel lblTrainerName = new JLabel("Trainer Name:");
		lblTrainerName.setFont(new Font("Tahoma", Font.BOLD, 14));
		lblTrainerName.setForeground(Color.WHITE);
		lblTrainerName.setBackground(new Color(0, 0, 0, 180));
		lblTrainerName.setOpaque(true);
		GridBagConstraints gbc_lblTrainerName = new GridBagConstraints();
		gbc_lblTrainerName.anchor = GridBagConstraints.EAST;
		gbc_lblTrainerName.insets = new Insets(0, 0, 5, 5);
		gbc_lblTrainerName.gridx = 2;
		gbc_lblTrainerName.gridy = 8;
		add(lblTrainerName, gbc_lblTrainerName);
		
		getTrainerName();
		trainerName.setEditable(false);
		GridBagConstraints gbc_trainerName = new GridBagConstraints();
		gbc_trainerName.fill = GridBagConstraints.HORIZONTAL;
		gbc_trainerName.gridwidth = 2;
		gbc_trainerName.insets = new Insets(0, 0, 5, 5);
		gbc_trainerName.gridx = 3;
		gbc_trainerName.gridy = 8;
		add(getTrainerName(), gbc_trainerName);
		trainerName.setColumns(10);
		
		JLabel lblContactNo = new JLabel("Contact no:");
		lblContactNo.setFont(new Font("Tahoma", Font.BOLD, 14));
		lblContactNo.setForeground(Color.WHITE);
		lblContactNo.setBackground(new Color(0, 0, 0, 180));
		lblContactNo.setOpaque(true);
		GridBagConstraints gbc_lblContactNo = new GridBagConstraints();
		gbc_lblContactNo.anchor = GridBagConstraints.EAST;
		gbc_lblContactNo.insets = new Insets(0, 0, 5, 5);
		gbc_lblContactNo.gridx = 2;
		gbc_lblContactNo.gridy = 9;
		add(lblContactNo, gbc_lblContactNo);
		
		getContactNo();
		GridBagConstraints gbc_contactNo = new GridBagConstraints();
		gbc_contactNo.fill = GridBagConstraints.HORIZONTAL;
		gbc_contactNo.gridwidth = 2;
		gbc_contactNo.insets = new Insets(0, 0, 5, 5);
		gbc_contactNo.gridx = 3;
		gbc_contactNo.gridy = 9;
		add(getContactNo(), gbc_contactNo);
		
		getBtnCreate();
		GridBagConstraints gbc_btnCreate = new GridBagConstraints();
		gbc_btnCreate.anchor = GridBagConstraints.EAST;
		gbc_btnCreate.insets = new Insets(0, 0, 5, 5);
		gbc_btnCreate.gridx = 5;
		gbc_btnCreate.gridy = 10;
		add(getBtnCreate(), gbc_btnCreate);
		
		JButton btnReset = new JButton("Reset");
		btnReset.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				category.setSelectedIndex(0);
				workshopName.setText("");
				description.setText("");
				venue.setText("");
				startDateTime.setValue((Date)CommonControls.addDayToDate(CommonControls.getCurrentDate(), 8));
				endDateTime.setValue((Date)CommonControls.addDayToDate(CommonControls.getCurrentDate(), 8));
				capacity.setValue(10);
				trainerNameCombo.setSelectedIndex(0);
				newTrainer.setSelected(false);
				trainerName.setText("");
				trainerName.setEditable(false);
				contactNo.setText("");
				contactNo.setEditable(false);
			}
		});
		GridBagConstraints gbc_btnReset = new GridBagConstraints();
		gbc_btnReset.insets = new Insets(0, 0, 5, 5);
		gbc_btnReset.gridx = 6;
		gbc_btnReset.gridy = 10;
		add(btnReset, gbc_btnReset);

		GridBagConstraints gbc_lblGrayBG = new GridBagConstraints();
		gbc_lblGrayBG.gridwidth = 8;
		gbc_lblGrayBG.gridheight = 12;
		gbc_lblGrayBG.gridx = 0;
		gbc_lblGrayBG.gridy = 0;
		add(getLblGrayBG(), gbc_lblGrayBG);
		
		GridBagConstraints gbc_lblBG = new GridBagConstraints();
		gbc_lblBG.gridwidth = 8;
		gbc_lblBG.gridheight = 12;
		gbc_lblBG.gridx = 0;
		gbc_lblBG.gridy = 0;
		add(getLblBG(), gbc_lblBG);
		
		this.setControl(new Workshop_CreateWorkshop_Controller(this));
	}
	
	public JLabel getLblWorkshopId() {
		if(lblWorkshopId == null){
			lblWorkshopId = new JLabel();
			lblWorkshopId.setForeground(Color.WHITE);
			lblWorkshopId.setBackground(new Color(0, 0, 0, 180));
			lblWorkshopId.setOpaque(true);
		}
		return lblWorkshopId;
	}

	public JTextField getWorkshopName() {
		if(workshopName == null){
			workshopName = new JTextField();
			workshopName.setBorder(BorderFactory.createLineBorder(Color.black, 2));
		}
		return workshopName;
	}

	public JTextField getTrainerName() {
		if(trainerName == null){
			trainerName = new JTextField();
			trainerName.setBorder(BorderFactory.createLineBorder(Color.black, 2));
		}
		return trainerName;
	}

	public JTextField getContactNo() {
		if(contactNo == null){
			contactNo = new JTextField();
			contactNo.setEditable(false);
			contactNo.setColumns(10);
			contactNo.setBorder(BorderFactory.createLineBorder(Color.black, 2));
		}
		return contactNo;
	}

	public JTextField getVenue() {
		if(venue == null){
			venue = new JTextField();                                                                                                          
			venue.setColumns(10);
			venue.setBorder(BorderFactory.createLineBorder(Color.black, 2));
		}
		return venue;
	}

	public JTextArea getDescription() {
		if(description == null){
			description = new JTextArea();
			description.setFont(new Font("Tahoma", Font.PLAIN, 13));
			description.setLineWrap(true);
			description.setBorder(BorderFactory.createLineBorder(Color.black, 2));
		}
		return description;
	}

	public JSpinner getStartDateTime() {
		if(startDateTime == null){
			startDateTime = new JSpinner();
			startDateTime.setBorder(BorderFactory.createLineBorder(Color.black, 2));
			startDateTime.setModel(new SpinnerDateModel(CommonControls.addDayToDate(CommonControls.getCurrentDate(), 8), CommonControls.addDayToDate(CommonControls.getCurrentDate(), 7), CommonControls.addYearToDate(CommonControls.getCurrentDate(), 1), Calendar.DAY_OF_YEAR));
			startDateTime.setEditor(new JSpinner.DateEditor(startDateTime, "dd/MM/yyyy HH:mm"));
		}
		return startDateTime;
	}

	public JSpinner getEndDateTime() {
		if(endDateTime == null){
			endDateTime = new JSpinner();
			endDateTime.setBorder(BorderFactory.createLineBorder(Color.black, 2));
			endDateTime.setModel(new SpinnerDateModel(CommonControls.addDayToDate(CommonControls.getCurrentDate(), 8), CommonControls.addDayToDate(CommonControls.getCurrentDate(), 7), CommonControls.addYearToDate(CommonControls.getCurrentDate(), 1), Calendar.DAY_OF_YEAR));
			endDateTime.setEditor(new JSpinner.DateEditor(endDateTime, "dd/MM/yyyy HH:mm"));
		}
		return endDateTime;
	}

	public JSpinner getCapacity() {
		if(capacity == null){
			capacity = new JSpinner();
			capacity.setBorder(BorderFactory.createLineBorder(Color.black, 2));
			capacity.setModel(new SpinnerNumberModel(10, 10, 30, 1));
		}
		return capacity;
	}
	
	public JComboBox getCategory() {
		if(category == null){
			category = new JComboBox();
			category.setBorder(BorderFactory.createLineBorder(Color.black, 2));
		}
		return category;
	}

	public JComboBox getTrainerNameCombo() {
		if(trainerNameCombo == null){
			trainerNameCombo = new JComboBox();
		}
		if(control != null){
			trainerNameCombo.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					control.setTrainer();
				}
			});
		}
		trainerNameCombo.setBorder(BorderFactory.createLineBorder(Color.black, 2));
		return trainerNameCombo;
	}

	public JCheckBox getNewTrainer() {
		if(newTrainer == null){
			newTrainer = new JCheckBox();
			newTrainer.setText("New Trainer");
			newTrainer.addItemListener(new ItemListener() {
				public void itemStateChanged(ItemEvent e) {
					control.setTrainerDetails();
				}
			});
		}
		return newTrainer;
	}
	
	public JButton getBtnCreate() {
		if(btnCreate == null){
			btnCreate = new JButton();
			btnCreate.setText("Create");
			btnCreate.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					int selection = JOptionPane.showConfirmDialog(null, "Are you sure you want to create?", "Confirmation", JOptionPane.YES_NO_OPTION); 
					if(selection == JOptionPane.YES_OPTION){
						control.createWorkshop();
					}
				}
			});
		}
		return btnCreate;
	}
	
	public JLabel getLblBG() {
		if (lblBG == null) {
			lblBG = new JLabel("");
			lblBG.setIcon(new ImageIcon(Toolkit.getDefaultToolkit().createImage("Resources/Images/Workshop/workshop1.gif").getScaledInstance(580, 350, Image.SCALE_DEFAULT)));
		}
		return lblBG;
	}
	public JLabel getLblGrayBG() {
		if (lblGrayBG == null) {
			lblGrayBG = new JLabel("");
			lblGrayBG.setPreferredSize(new Dimension(560, 340));
			lblGrayBG.setBackground(new Color(0, 0, 0, 180));
			lblGrayBG.setOpaque(true);
		}
		return lblGrayBG;
	}
}
